/* 
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package com.avdheshyadav.p4j.jdbc.dbms;

import org.apache.log4j.Logger;

import com.avdheshyadav.p4j.common.DbmsType;
import com.avdheshyadav.p4j.jdbc.PersistenceConfig;
import com.avdheshyadav.p4j.jdbc.common.CRUD;
import com.avdheshyadav.p4j.jdbc.common.GenericCRUD;
import com.avdheshyadav.p4j.jdbc.dao.AbsPersistenceManager;
import com.avdheshyadav.p4j.jdbc.dao.DefaultPersistenceManager;
import com.avdheshyadav.p4j.jdbc.dbms.connfactory.ConnectionFactory;
import com.avdheshyadav.p4j.jdbc.dbms.connfactory.DBConnector;
import com.avdheshyadav.p4j.jdbc.dbms.metadata.IMetaDataLoader;
import com.avdheshyadav.p4j.jdbc.dbms.metadata.MetaDataLoader;

/**
 * 
 * @author Avdhesh Yadav
 *
 */
public class HsqlDbms extends Dbms
{
	static Logger logger = Logger.getLogger(DerbyDbms.class.getName());
	
	private static Dbms _Instance;

	/**
	 * 
	 * @param props PersistenceConfig
	 * 
	 * @return Dbms
	 */
	public static Dbms getInstance(PersistenceConfig config)
	{
		if(_Instance == null)
		{
			_Instance = new HsqlDbms(config);
		}
		return _Instance;
	}
	
	
	/**
	 * 
	 * @param config PersistenceConfig
	 */
	private HsqlDbms(PersistenceConfig config)
	{
		super(config);
		m_ConnFactory = ConnectionFactory.getConnectionFactory(DbmsType.HSQLDB);
		mDsAttr = constructDataSourceAttr(DbmsType.HSQLDB);
	}
	
	
	/**
	 * 
	 * @param database String
	 * 
	 * @throws Exception
	 */
	public void createDataSource(String database, boolean isTransactional) throws Exception
	{
		logger.info("createDataSource in HSQLDB:" + database);
		
		boolean isNew = isNewDataSource(database);
		
		if(isNew)
		{
			DBConnector connector = getDBConnector(database, isTransactional);
			IMetaDataLoader loader = new MetaDataLoader(connector);
			CRUD crud = new GenericCRUD();
			AbsPersistenceManager pcManager = new DefaultPersistenceManager(loader, crud);
			m_PcManagerMap.put(database, pcManager);
		}
	}
}
